package com.academic.system;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * 高校教师学术成果信息综合管理系统
 * Spring Boot 应用主类
 * 
 * 启用了以下功能:
 * - 缓存支持：提高重复数据查询性能
 * - 异步执行：提高系统响应能力
 * - 定时任务：支持系统维护和定期任务
 * - Servlet组件扫描：支持自定义Servlet、Filter等
 */
@SpringBootApplication(exclude = {
    org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class,
    org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration.class
})
@EnableCaching
@EnableAsync
@EnableScheduling
@ServletComponentScan
public class AcademicSystemApplication {

    /**
     * 配置全局CORS跨域支持
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 允许特定来源
        config.addAllowedOriginPattern("http://localhost:8081");
        config.addAllowedOriginPattern("http://127.0.0.1:8081");
        // 允许所有头信息
        config.addAllowedHeader("*");
        // 允许暴露的响应头
        config.setExposedHeaders(Arrays.asList(
            "Set-Cookie",
            "Authorization", 
            "Content-Type"
        ));
        // 允许所有方法
        config.addAllowedMethod("*");
        // 允许凭证
        config.setAllowCredentials(true);
        // 预检请求的有效期，单位为秒
        config.setMaxAge(3600L);
        
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        
        return new CorsFilter(source);
    }

    public static void main(String[] args) {
        SpringApplication.run(AcademicSystemApplication.class, args);
    }
} 